package hotel.quartz.oversea.job.task;

import hotel.base.oversea.constant.CallbackConstants;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.constant.CommonUrlConstants;
import hotel.base.oversea.constant.enums.MessageEnums;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.GsonUtils;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.utils.SendUtils;
import hotel.base.oversea.vo.meituan.response.goods.MtfpHotelGoodsCancelRuleVo;
import hotel.quartz.oversea.config.service.RedisService;
import hotel.quartz.oversea.util.JobRestTemplateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Component
public class SwUpdateCancelTask {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RedisService redisService;

    public static void main(String[] args) {
        Long size = 663L;
        if(size>0){
            System.out.printf("==="+size);
        }
    }

    //@Async 标识这个方法异步执行，可返回void或Future
    @Async
    public void executeUpdateCancel(){
        log.info("Sw Update Cancel Task 开始执行：{}", DateUtil.getCurrTime());
        Long size = redisService.queueSize(CallbackConstants.REDIS_SWMT_QUEUE_CHANGE_CANCEL_GOODS);
        log.info("Sw Update Cancel Task queueSize：{}", size);
        if(size>0){
            List<MtfpHotelGoodsCancelRuleVo> array = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                String json = (String)redisService.RPop(CallbackConstants.REDIS_SWMT_QUEUE_CHANGE_CANCEL_GOODS);
                if(null != json){
                    log.info("Sw Update Cancel Task json：{}", json);
                    MtfpHotelGoodsCancelRuleVo mtfpHotelGoodsCancelRuleVo = GsonUtils.GsonToBean(json, MtfpHotelGoodsCancelRuleVo.class);
                    if(null != mtfpHotelGoodsCancelRuleVo){
                        array.add(mtfpHotelGoodsCancelRuleVo);
                        if(array.size()==6){
                            log.info("Sw Update Cancel Task array.size()==6 请求携程推送取消vo：{}", GsonUtils.GsonString(array));
                            req(array);
                            array = new ArrayList<>();
                            try {
                                Thread.sleep(500);
                            } catch (Exception e2) {
                                e2.printStackTrace();
                            }
                        }
                    }else{
                        log.info("Sw Update Cancel Task 转化数据MtfpHotelGoodsCancelRuleVo为空json：{}", json);
                    }
                }else{
                    log.info("Sw Update Cancel Task 队列没有下一条了，array：{}", array==null?0:array.size());
                    if(null != array && array.size()>0){
                        req(array);
                    }
                    break;
                }
            }
            log.info("Sw Update Cancel Task 循环size结束判断array：{}", array==null?0:array.size());
            if(null != array && array.size()>0){
                req(array);
            }

        }

        log.info("Sw Update Cancel Task 结束执行：{}", DateUtil.getCurrTime());

    }

    public void req(List<MtfpHotelGoodsCancelRuleVo> list){
        if(null != list && list.size()>0){
            Map<String,Object> map = new HashMap<>();
            map.put("cancelRuleList",list);
            log.info("SwUpdateCancelTask 调用携程更新取消规则，请求参数："+ GsonUtils.GsonString(map));
            Result response = JobRestTemplateUtils.post(CommonUrlConstants.SWITCHS_ONLINE_HOST+"/switchCreateRoom/pushRoomCancelRule", GsonUtils.GsonString(map),restTemplate);
            if(response.getCode()!= CommonConstants.SUCCESS){
                SendUtils.dingMsgNotLog("[SW-UPDATE-CANCEL] 调用携程取消规则更新失败 ", DateUtil.getCurrTime()+" 请求参数："+ GsonUtils.GsonString(map)+" 返回参数："+ GsonUtils.GsonString(response), MessageEnums.genre.GOODS_STATUS_AND_PRICE.getCode(),restTemplate);
            }
            log.info("SwUpdateCancelTask 调用携程更新取消规则，返回参数："+ GsonUtils.GsonString(response));
        }
    }

}
